import { handlerGobackClick } from "../../../utils/util";
import { Subscribe } from "../../../models/subscribe";
import { Renter } from "../../../models/renter";
import { Pets } from "../../../models/pets";
const subscribe = new Subscribe();

const renter = new Renter();

const pets = new Pets();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //选择类型
    type: "",
    radio: "",
    //查询参数表示,就是id
    phone: "",
    communityList: "",
    prevPage: null,
    titleNavName: "",

    //选择映射关系
    mapping: {
      selectCommunity: {
        //选择的id
        name: "commid",
        //显示的字段
        title: "commName",
      },
      renterSelectCommunity: {
        name: "commid",
        title: "comm_name",
      },
      renterSelectBuilding: {
        name: "building_id",
        title: "building_name",
      },
      renterSelectHous: {
        name: "house_id",
        title: "house_name",
      },
      petSex: {
        name: "value",
        title: "title",
      },
      petKind: {
        name: "code",
        title: "name",
      },
      petVariety: {
        name: "code",
        title: "name",
      },
      petType: {
        name: "code",
        title: "name",
      },
    },
  },
  onLoad: function (options) {
    const { type } = options;
    if (type) {
      this.setData({
        type: type,
      });
    }
    if (type === "selectCommunity") {
      this.setData({
        phone: options.phone,
        radio: options.commid,
      });
    } else if (type === "renterSelectCommunity") {
      this.setData({
        radio: options.commid,
        titleNavName: "小区名称",
      });
    } else if (type === "renterSelectBuilding") {
      this.setData({
        phone: options.phone,
        radio: options.buildingId,
        titleNavName: "楼号选择",
      });
    } else if (type === "renterSelectHous") {
      this.setData({
        phone: options.phone,
        radio: options.housId,
        titleNavName: "房号选择",
      });
    }

    //宠物性别
    else if (type === "petSex") {
      this.setData({
        radio: options.phone,
        titleNavName: "宠物性别",
      });
    }
    //种类
    else if (type === "petKind") {
      this.setData({
        radio: options.phone,
      });
    }
    //品种
    else if (type === "petVariety") {
      this.setData({
        radio: options.code,
        phone: options.phone,
      });
    }
    //类型
    else if (type === "petType") {
      this.setData({
        radio: options.code,
        phone: options.phone,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  onShow: function () {
    let curPages = getCurrentPages(); //获取当前页面js里的所有信息
    let prevPage = curPages[curPages.length - 2];
    this.setData({
      prevPage,
    });
    this.initPageData();
  },

  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  handlerGobackClick,
  onClick(event) {
    const { name, title } = event.currentTarget.dataset;
    const { type } = this.data;
    this.setData({
      radio: name,
    });
    if (type === "selectCommunity") {
      this.data.prevPage.setData({
        commid: name,
        communityName: title,
      });
    } else if (type === "renterSelectCommunity") {
      this.data.prevPage.setData({
        commId: name,
        commIdName: title,
      });

      this.setData({
        titleNavName: "小区名称",
      });
    } else if (type === "renterSelectBuilding") {
      this.data.prevPage.setData({
        buildingId: name,
        buildingIdName: title,
      });

      this.setData({
        titleNavName: "楼号选择",
      });
    } else if (type === "renterSelectHous") {
      this.data.prevPage.setData({
        housId: name,
        housIdName: title,
      });

      this.setData({
        titleNavName: "房号选择",
      });
    }
    //宠物性别
    else if (type === "petSex") {
      this.data.prevPage.setData({
        sex: name,
        sexName: title,
      });

      this.setData({
        titleNavName: "宠物性别",
      });
    }
    //种类
    else if (type === "petKind") {
      this.data.prevPage.setData({
        kind: name,
        kindName: title,
      });
    }
    //品种
    else if (type === "petVariety") {
      this.data.prevPage.setData({
        variety: name,
        varietyName: title,
      });
    }
    //类型
    else if (type === "petType") {
      this.data.prevPage.setData({
        type: name,
        typeName: title,
      });
    }
    wx.navigateBack();
  },
  initPageData() {
    const { type } = this.data;
    console.log(type);
    if (type === "selectCommunity") {
      subscribe
        .getOwnerCommunity(this.data.phone)
        .then((res) => {
          console.log("这是mmmmm我的社区", res);
          this.setData({
            communityList: res.messageBody,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    } else if (type === "renterSelectCommunity") {
      renter
        .houseList()
        .then((res) => {
          this.setData({
            communityList: res.result,
            titleNavName: "小区名称",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    } else if (type === "renterSelectBuilding") {
      renter
        .houseList(this.data.phone)
        .then((res) => {
          this.setData({
            communityList: res.result,
            titleNavName: "楼号选择",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    } else if (type === "renterSelectHous") {
      renter
        .houseList(undefined, this.data.phone)
        .then((res) => {
          this.setData({
            communityList: res.result,
            titleNavName: "房号选择",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    }
    //宠物性别
    else if (type === "petSex") {
      pets
        .getSex()
        .then((res) => {
          this.setData({
            communityList: res.result,
            titleNavName: "宠物性别",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    }
    //种类
    else if (type === "petKind") {
      pets
        .getKind()
        .then((res) => {
          this.setData({
            communityList: res.result,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    }
    //品种
    else if (type === "petVariety") {
      pets
        .getVariety(this.data.phone)
        .then((res) => {
          this.setData({
            communityList: res.result,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    }
    //类型
    else if (type === "petType") {
      pets
        .getType(this.data.phone)
        .then((res) => {
          this.setData({
            communityList: res.result,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
});
